﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.Composition;
using UnfriendlyFire.Models;

namespace UnfriendlyFire.Controllers
{
    [Export(typeof(IController))]
    [ExportMetadata("Name", "ArmedForce")]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
    public class ArmedForceController : Controller
    {
        private GameRepository _gameRepository;

        [ImportingConstructor]
        public ArmedForceController(GameRepository gameRepository)
        {
            _gameRepository = gameRepository;
        }

        public ActionResult Index(int? offset, int? count)
        {
            var armedForces = _gameRepository.AuthoredArmedForces.OrderBy(a => a.ArmedForceId).Skip(offset.HasValue ? offset.Value : 0).Take(count.HasValue ? count.Value : 100);
            return View(armedForces);
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Create()
        {
            return View(ArmedForce.CreateArmedForce(0, null, "A cool name", "A compelling description", 0, "/Content/Images/DefaultArmedForce.png"));
        }

        [HttpPost]
        [Authorize]
        public ActionResult Create(ArmedForce armedForce)
        {
            if (!ModelState.IsValid)
            {
                return View(armedForce);
            }
            try
            {
                armedForce.Author = _gameRepository.Officers.First(o => o.Email == HttpContext.User.Identity.Name);
                _gameRepository.ArmedForces.AddObject(armedForce);
                _gameRepository.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e);
                return View(armedForce);
            }
        }

        public ActionResult Edit(int id)
        {
            var armedForce = _gameRepository.ArmedForces.First(a => a.Id == id);
            return View(armedForce);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ArmedForce/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /ArmedForce/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
